package com.orhonit.ipb.portal.rest;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.orhonit.ipb.portal.biz.JoEnclosureBiz;
import com.orhonit.ipb.portal.configuration.UploadConfig;
import com.orhonit.ipb.portal.configuration.UploadConfigTemp;
import com.orhonit.ipb.portal.entity.JoEnclosure;
import com.orhonit.ipb.portal.util.FileUtils;
import com.orhonit.ipb.security.auth.client.annotation.CheckClientToken;
import com.orhonit.ipb.security.auth.client.annotation.CheckUserToken;
import com.orhonit.ipb.security.common.rest.BaseController;
import com.orhonit.ipb.security.common.util.UUIDUtils;

@RestController
@RequestMapping("joEnclosure")
//@CheckClientToken
//@CheckUserToken
public class JoEnclosureController extends BaseController<JoEnclosureBiz,JoEnclosure> {
	@Autowired
	private JoEnclosureBiz joEnclosureBiz;
	@Autowired
	private UploadConfig uploadConfig;
	/**
	 * 通过申请id及标签下载附件
	 * @param applyId
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/getjoJoinInfo", method = RequestMethod.GET)
	public ResponseEntity<?> getbaseUserInfoByself(String applyId,String label,HttpServletResponse response){
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("joinId", applyId);
		map.put("label", label);
		JoEnclosure joinInfo=joEnclosureBiz.getJoEnclosure(map);
		String filepath=joinInfo.getValue();
		File file1 = new File(filepath);
		String[] split = filepath.split("\\.");
		boolean flag = FileUtils.downLoadFile(filepath,file1.getName(),split[split.length - 1], response, request);
		if (!flag) {
			return ResponseEntity.status(402).body("下载失败");
		}
		return ResponseEntity.ok(null);
	}
	/**
	 * 通过申请id及标签查看是否有附件
	 * @param applyId
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/getjoJoinInfoexitst", method = RequestMethod.GET)
	public ResponseEntity<?> getjoJoinInfoexitst(String applyId,String label){
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("joinId", applyId);
		map.put("label", label);
		JoEnclosure joinInfo=joEnclosureBiz.getJoEnclosure(map);
		if(joinInfo==null){
			return ResponseEntity.ok(null);
		}
		else{
		   return ResponseEntity.ok().body("1");
		}
		
	}
	/**
	 * 通过申请id及标签上传附件
	 * @param applyId
	 * @param label
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/addjoJoinInfo", method = RequestMethod.POST)
	public ResponseEntity<?> addjoJoinInfo(@RequestParam("file") MultipartFile file,String applyId,String label) throws IllegalStateException, IOException{
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("joinId", applyId);
		map.put("label", label);
		JoEnclosure exitst=joEnclosureBiz.getJoEnclosure(map);
		if(exitst==null){
			long time = System.currentTimeMillis();  
			File filePath = new File(uploadConfig.getPath());//  /api/admin/portal/userInfo/
			String fileurl=filePath+File.separator+time+file.getOriginalFilename();
			if (null != file && file.getSize() > 0) {
			filePath.mkdirs();
			filePath = new File(fileurl);
			file.transferTo(filePath);
			}
			JoEnclosure joinInfo=new JoEnclosure();
			joinInfo.setApplyId(applyId);
			joinInfo.setLabel(label);
			joinInfo.setValue(fileurl);
			joinInfo.setId(UUIDUtils.generateUuid());
			joEnclosureBiz.insertSelective(joinInfo);
			return ResponseEntity.ok().body(null);
		}
		else{
			File files=new File(exitst.getValue());
			files.delete();
			long time = System.currentTimeMillis();  
			File filePath = new File(uploadConfig.getPath());//  /api/admin/portal/userInfo/
			String fileurl=filePath+File.separator+time+file.getOriginalFilename();
			if (null != file && file.getSize() > 0) {
			filePath.mkdirs();
			filePath = new File(fileurl);
			file.transferTo(filePath);
			}
			exitst.setValue(fileurl);
			joEnclosureBiz.updateById(exitst);
			return ResponseEntity.ok().body(null);
		}
	}
	/**
	 * 通过申请id及标签上传附件
	 * @param applyId
	 * @param label
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/addjoJoinInfoimage", method = RequestMethod.POST)
	public ResponseEntity<?> addjoJoinInfoimage(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
			long time = System.currentTimeMillis();  
			File filePath = new File(uploadConfig.getPath());//  /api/admin/portal/userInfo/
			String fileurl=time+file.getOriginalFilename();
			if (null != file && file.getSize() > 0) {
			filePath.mkdirs();
			filePath = new File(filePath+File.separator+fileurl);
			file.transferTo(filePath);
			}
			return ResponseEntity.ok().body(uploadConfig.getServerUrl()+":7789/Image/"+fileurl);
	}
}